package org.tpdata.api.singlequality.qcindicator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.*;
import exceptions.TestNGRetry;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.*;

@Feature("增删查改预警设置")
public class CRUDWarnConf extends SetUpTearDown1 {
    Logger log = Logger.getLogger(CRUDWarnConf.class);
    Data data = envSetUp();
    public  void CreateConf() {
//    Map<String, String> json = new HashMap<String, String>();
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("userId", data.getUserId());
        json.put("medCode", "COPD-001");     //  报告号，多个值以逗号连接  值base_copd_01
        json.put("reportNo", "03002");     //住院性别分析
        json.put("lower", "1");     //下限
        json.put("upper", "100");     //上限
        log.info(json.toString());
//    json.put("id", "580453354166161408"); //reportNo的指标ID
//    json.put("hospitalCode", "12440100455344205E");  //医院
        Response response = RunCaseJson.runCase(json, "post", "api-sourcedata/reportWarning/saveConfig.json");
        response.prettyPrint();
    }
    public  String FindConf() {
//        Data data = envSetUp();
        JSONObject json2 = new JSONObject();
        json2.put("access_token", data.getAccessToken());
        json2.put("authToken", data.getAccessToken());
        json2.put("uniqu_no", data.getUniquNo());
        json2.put("operatorId", data.getUserId());
        json2.put("authUserId", data.getUserId());
        json2.put("medCode", "COPD-001");     //  报告号，多个值以逗号连接  值base_copd_01
        json2.put("multiCenter", "0");
        json2.put("userId", data.getUserId());
        Response response2 = RunCaseJson.runCase(json2, "get", "api-sourcedata/reportWarning/findConfig.json");
        response2.prettyPrint();
        JSONObject obj = JSONObject.parseObject(response2.asString());
        JSONArray contentArr = obj.getJSONArray("responseData");
        String Id = contentArr.getJSONObject(0).getString("id");
        return Id;
    }
    @Story("增删查改预警设置")
    @BeforeTest
    public  void beforetest(){
        CreateConf();
    }
    @Parameters({"medCode"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "")
    public void SaveConf(String medCode) {
        JSONObject json = new JSONObject();
        String id=  FindConf();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("userId", data.getUserId());
        json.put("medCode", medCode);     //  报告号，多个值以逗号连接  值base_copd_01
        json.put("reportNo", "03002");     //住院性别分析
        json.put("lower", "2");     //下限
        json.put("upper", "50");     //上限
        json.put("id", id); //reportNo的指标ID
//    json.put("hospitalCode", "12440100455344205E");  //医院
        Response response = RunCaseJson.runCase(json, "post", "api-sourcedata/reportWarning/saveConfig.json");
        log.info("响应报文：" + response.prettyPrint());
        JsonPath jsonPath=response.jsonPath();
       String responseResult= jsonPath.getString("responseResult");
        Assert.assertEquals(responseResult,"SUCCESS");

    }

    @AfterTest()
    public void DeleteConf() {
        JSONObject json = new JSONObject();
        String id=  FindConf();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("id", id); //reportNo的指标ID
        Response response = RunCaseJson.runCase(json, "post", "api-sourcedata/reportWarning/deleteConfig.json");
    }

}
